package com.scenic.rd_android.testview.lstview;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.nova.scenic.projectlibs.util.adapter.SimpleAdapter;
import com.nova.scenic.projectlibs.util.debug.MyLog;
import com.scenic.rd_android.R;

import java.util.ArrayList;
import java.util.List;

public class TestCustomListView extends AppCompatActivity {

    private MyCustomListView listView;

    MyLog myLog = new MyLog(TestCustomListView.class.getSimpleName());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_custom_list_view);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        listView = (MyCustomListView) findViewById(R.id.id_test_listView);
        listView.setAdapter(new MainListAdapter1());
    }


    class ItemDataEntity {
        public int type;
        public String data;
    }

    class MainListAdapter1 extends BaseAdapter {
        List<ItemDataEntity> data = new ArrayList<>();

        int TypeCount = 2;

        MainListAdapter1() {
            for (int i = 0; i < 100; i++) {
                ItemDataEntity entity = new ItemDataEntity();
                entity.data = "item " + i;
                if(i == 3){
                    entity.type = 1;
                }else {
                    entity.type = 0;
                }
                data.add(entity);
            }
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getViewTypeCount() {
            return TypeCount;
        }

        @Override
        public int getItemViewType(int position) {

            return data.get(position).type;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            switch (getItemViewType(position)){
                case 0:
                    if (convertView == null) {
                        convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.adapter_custom_listview_test, null);
                    }
                    TextView txView = (TextView) convertView.findViewById(R.id.id_textview_item1);
                    txView.setText(data.get(position).data);

                    break;
                case 1:
                    if (convertView == null) {
                        convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.adapter_custom_listview_type_2, null);
                        HorizontalListView listView = (HorizontalListView) convertView.findViewById(R.id.id_item_horizontal_listview);
                        listView.setAdapter(new SimpleAdapter<>(getApplicationContext(), stringData));
                        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Toast.makeText(getApplicationContext(),"click the valie " + parent.getAdapter().getItem(position),Toast.LENGTH_LONG ).show();
                            }
                        });
                    }


                    break;
            }


            convertView.setTag(position);
            boolean attach = convertView.isAttachedToWindow();
            Rect rect = new Rect();
            convertView.getGlobalVisibleRect(rect);
            myLog.debug("rect " + rect + "   " +  convertView.isActivated() + "  " + position);
            return convertView;
        }
    }

    List<String> stringData = new ArrayList<>();
    {
        for(int i = 0;i < 100; i ++ ){
            stringData.add("Item " + i);
        }
    }

}
